//Painel para cadastro de grupos
//Exibe os campos para cadastramento e um grafico de pizza 
//Mostrando a divisão da renda pelos grupos cadastrados
package fmu.pi.view.draw.old;

import javafx.geometry.Pos;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox; 
import javafx.scene.shape.Rectangle; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.control.TextField;
import javafx.scene.control.Label;  
import javafx.scene.control.ListView; 
import javafx.scene.control.Button;  
import javafx.scene.chart.PieChart;
import java.util.ArrayList; 
import fmu.pi.model.bean.Grupo;
import fmu.pi.view.handler.HandlerView;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javax.swing.JOptionPane; 

public class CadastraGrupo extends BorderPane
{
    private Label grupoLabel; 
    private TextField grupoTextField; 
    private TextField alteraNomeTextField; 
    private Button saveButton; 
    private Button alterButton; 
    private Button cancelaButton; 
    private Button deletaButton; 
    private PieChart pieChart; 
    private ListView <Grupo> grupoListView; 
    private GridPane gridAltera;
    private HandlerView handler;
    private Grupo grupoSelected; 
    
    //construtor
    public CadastraGrupo()
    {
         
        initPane(); 
    }
    
    public void initPane()
    {
        handler = new HandlerView();
        setCenter(pane()); 
    }
    
    private GridPane pane()
    {
        GridPane grid = new GridPane(); 
        grid.setHgap(10); 
        grid.setVgap(10); 
        grid.add(new Rectangle(75, 30, Color.TRANSPARENT), 2, 1); 
        grid.add(layoutCadastra(), 1, 1); 
        grid.add(paneAltera(), 3, 1); 
        grid.add(pieChart(), 1, 2);
        
        return grid; 
    }
    
    //box com TextField para cadastro e Choice box para escolha do tipo de conta
    private GridPane layoutCadastra()
    {
        GridPane grid = new GridPane(); 
        grid.setHgap(10); 
        grid.setVgap(10);
        
        for(int i = 0; i < 4; i++)
            grid.add(new Rectangle(15, 30, Color.TRANSPARENT), 0, i);
     
        for(int i = 0; i < 4; i++)
            grid.add(new Rectangle(200, 30, Color.TRANSPARENT), 1, i);
        
        
        grupoLabel = new Label(); 
        grupoLabel.setText("Cadastro de Grupos"); 
        grupoLabel.setFont(Font.font("Ariel", 20)); 
        grupoLabel.setTextFill(Color.DARKBLUE);
        grid.add(grupoLabel, 1, 1); 
        
        grupoTextField = new TextField(); 
        grupoTextField.setMaxSize(300, 30);
        grupoTextField.setText("");
        grid.add(grupoTextField,1, 2); 
        
        saveButton = new Button(); 
        saveButton.setText("Salvar"); 
        saveButton.setMaxSize(60, 22); 
        saveButton.setOnAction(new CadastraGrupo.SalveButtonHandler()); 
        grid.add(saveButton, 1, 3); 
        
        return grid; 
    }
   
    private HBox paneAltera()
    {   
        
        HBox box =  new HBox(); 
        box.setSpacing(10); 
        box.setAlignment(Pos.CENTER);
                
        gridAltera = new GridPane();
        gridAltera.setAlignment(Pos.CENTER);
        gridAltera.setHgap(10); 
        gridAltera.setVgap(10);
        gridAltera.setVisible(false); 
        
        Label grupoLabel = new Label(); 
        grupoLabel.setText("Grupos"); 
        grupoLabel.setFont(Font.font("Ariel Black", 20)); 
        grupoLabel.setTextFill(Color.DARKBLUE); 
        
        for(int i = 0; i < 3; i++)
            gridAltera.add(new Rectangle(30, 15, Color.TRANSPARENT), i, 0); 
        
        for(int i = 0; i < 3; i++)
            gridAltera.add(new Rectangle(30, 15, Color.TRANSPARENT), 0, i); 
        
        alteraNomeTextField = new TextField(); 
        alteraNomeTextField.setMaxSize(100, 30); 
        gridAltera.add(alteraNomeTextField, 1, 1); 
        
        alterButton = new Button();
        alterButton.setText("Alterar"); 
        alterButton.setMaxSize(60, 30); 
        alterButton.setOnAction(new CadastraGrupo.UpdateButtonHandler()); 
        gridAltera.add(alterButton, 2, 1);
        
        deletaButton = new Button(); 
        deletaButton.setText("Apagar"); 
        deletaButton.setMaxSize(60, 30); 
        deletaButton.setOnAction(new CadastraGrupo.DeletButtonHandler()); 
        
        gridAltera.add(deletaButton, 2, 2); 
        cancelaButton = new Button(); 
        cancelaButton.setText("Cancelar"); 
        cancelaButton.setMaxSize(60, 30); 
        cancelaButton.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                gridAltera.setVisible(false); 
            }
        }); 
        gridAltera.add(cancelaButton,2 ,3); 
        
        grupoListView = createListGroup((ArrayList<Grupo>)handler.getGroupList()); 
        box.getChildren().addAll(grupoLabel, grupoListView, gridAltera);
        
        return box; 
    }
    
    private PieChart pieChart()
    {
       ArrayList<Grupo> groupName = new ArrayList<Grupo>(); 
        
       pieChart = new PieChart();  
        
        
        
        pieChart.setMaxSize(300, 300); 
        pieChart.setTitle("Saúde financeira"); 
        
        
        return pieChart; 
    }
    
    private ListView createListGroup(ArrayList<Grupo> grupoL)
    {
        final ListView<Grupo> groupLV = new ListView<Grupo>(); 
        groupLV.setMaxSize(100, 125); 
        
        ObservableList grupos = FXCollections.observableArrayList(grupoL); 
        groupLV.setItems(grupos); 
        
        groupLV.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {   
                grupoSelected = (Grupo) groupLV.getSelectionModel().getSelectedItem();
                gridAltera.setVisible(true); 
                alteraNomeTextField.setText(grupoSelected.getNome()); 
            }
        });
        
        return groupLV; 
    }
    
    private class SalveButtonHandler implements EventHandler<ActionEvent>
    {      
        @Override
        public void handle(ActionEvent event)
        {
            Integer result = 0; 
            if((grupoTextField.getText() != null) 
                    && (!grupoTextField.getText().equals("")))
            {
                Grupo g = new Grupo(); 
                g.setNome(grupoTextField.getText()); 
                result =  handler.updateTables(g, "insert");
                
                if(result == 0)
                    JOptionPane.showMessageDialog(null, 
                            "Grupo inserido com sucesso"); 
                
                handler = new HandlerView(); 
                initPane(); 
                alteraNomeTextField.setText("");
                grupoTextField.setText(""); 
            }
        }
    }
    
    private class UpdateButtonHandler implements EventHandler<ActionEvent>
    {   
        @Override
        public void handle(ActionEvent event)
        {
            Integer result = 0; 
            
            if((alteraNomeTextField.getText() != null) 
                && (!alteraNomeTextField.getText().equals("")))
            {
   
                grupoSelected.setNome(alteraNomeTextField.getText());
                result =  handler.updateTables(grupoSelected, "update");
                
                if(result == 0)
                    JOptionPane.showMessageDialog(null, 
                        "Grupo alterado com sucesso");
                handler = new HandlerView(); 
                initPane(); 
                alteraNomeTextField.setText(""); 
            }
        }
    }
   
    private class DeletButtonHandler implements EventHandler<ActionEvent>
    {
        @Override
        public void handle(ActionEvent event)
        {
            Integer result = 0; 
            String message = "Todas as contas relacionadas ao grupo e todas"
                    +" as movimentações serão apagadas, deseja continuar?";
            
            Integer option = JOptionPane.showConfirmDialog(null, message);
            
            if(option == 0)
            {
                result = handler.updateTables(grupoSelected, "delet"); 
                if(!(result > 0))
                    JOptionPane.showMessageDialog(null, "Erro ao apagar grupo."); 
                else
                {
                    initPane();
                    JOptionPane.showMessageDialog(null, 
                            "Grupo apagado com sucesso."); 
                }
            }
        }
    }
    
}
